﻿/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#include "../import/head.h"
#include "../macro/head.h"
#include "package_impl.h"
#include "all_impl.h"
#include "hook.h"
#include "mgr.h"

namespace xos_package
{

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    static xos_compress::i_compress_mgr * compress_ptr = 0;
    static xos_container::i_container * container_ptr = 0;
    static xos_encrypt::i_encrypt_mgr * encrypt_ptr = 0;
    static xos_common::i_common * common_ptr = 0;
    static xos_log::i_log * log_ptr = 0;
    static xos::i_xos * xos_ptr = 0;

    static xos_box::i_serial * serial_ptr = 0;
    static xos::i_crt * crt_ptr = 0;

    static mgr * package_mgr_ptr = 0;

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    mgr::mgr()
    {
    }

    mgr::~mgr()
    {
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    int mgr::static_init( xos_container::i_container * pContainer, i_package_mgr ** ppv )
    {
        int ret = 0;

        mgr::T * pObj = 0;

        if( ( 0 == ret ) && package_mgr_ptr )
        {
            ret = 1;
        }

        if( 0 == ret )
        {
            container_ptr = pContainer;
            compress_ptr = pContainer->compress();
            encrypt_ptr = pContainer->encrypt();
            common_ptr = pContainer->common();
            xos_ptr = pContainer->xos();
            log_ptr = pContainer->log();
        }

        if( 0 == ret )
        {
            pObj = new mgr::T;
            if( pObj )
            {
                package_mgr_ptr = pObj;
            }
            else
            {
                ret = 1;
            }
        }

        if( 0 == ret )
        {
            ret = pObj->init();
            if( 0 != ret )
            {
                ret = 1;
            }
        }
        
        if( 0 != ret )
        {
            package_mgr_ptr = 0;
        }
        else
        {
            *ppv = pObj;
            pObj = 0;
        }

        xos_stl::release_interface( pObj );

        return ret;
    }

    xos_container::i_container * mgr::container()
    {
        return container_ptr;
    }

    xos_encrypt::i_encrypt_mgr * mgr::encrypt()
    {
        return encrypt_ptr;
    }

    xos_compress::i_compress_mgr * mgr::compress()
    {
        return compress_ptr;
    }

    xos_common::i_common * mgr::common()
    {
        return common_ptr;
    }

    xos_box::i_serial * mgr::serial()
    {
        return serial_ptr;
    }

    xos_common::i_md5 * mgr::md5()
    {
        return container()->md5();
    }

    xos_log::i_log * mgr::log()
    {
        return log_ptr;
    }

    xos::i_crt * mgr::crt()
    {
        return crt_ptr;
    }

    xos_box::i_big_buf * mgr::big_buf()
    {
        return container_ptr->big_buf();
    }

    xos_box::i_buf * mgr::buf()
    {
        return container_ptr->buf();
    }

    xos::i_xos * mgr::xos()
    {
        return xos_ptr;
    }

    mgr * mgr::get()
    {
        return package_mgr_ptr;
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    // 
    // 创建package功能接口
    // 
    int mgr::create( enumType eType, void ** ppv )
    {
        int ret = 0;

        switch( eType )
        {
        case enumType::PACKAGE:
            {
                package_impl * pRet = package_impl::get_item_from_pool( true );
                pRet->init();
                *ppv = pRet;
            }
            break;
        case enumType::ALL:
            {
                all_impl * pRet = all_impl::get_item_from_pool( true );
                pRet->init();
                *ppv = pRet;
            }
            break;
        default:
            {
                ret = 1;
            }
            break;
        }

        return ret;
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    int mgr::put_back_to_pool( T * pT, bool bLock )
    {
        int ret = 0;
        delete this;
        return ret;
    }

    int mgr::init()
    {
        int ret = 0;

        if( 0 == ret )
        {
            log()->add_log_module( PKGE_LOG, 1 );
            log()->add_log_module( PKGE_LOG, 2 );
            log()->add_log_module( PKGE_LOG, 3 );
            log()->add_log_module( PKGE_LOG, 4 );
        }

        if( 0 == ret )
        {
            ret = hook::init();
        }

        if( 0 == ret )
        {
            serial_ptr = container()->serial();
        }

        if( 0 == ret )
        {
            crt_ptr = container()->crt();
        }

        return ret;
    }

    int mgr::term()
    {
        int ret = 0;

        hook::term();

        package_mgr_ptr = 0;
        container_ptr = 0;
        compress_ptr = 0;
        encrypt_ptr = 0;
        common_ptr = 0;
        xos_ptr = 0;
        log_ptr = 0;
        serial_ptr = 0;
        crt_ptr = 0;

        return ret;
    }

} // xos_package
